﻿using IdentityServer4;
using IdentityServer4.Test;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;

namespace Ids4Extensions
{
    public static class IdentityServer4Extension
    {
        public static IServiceCollection AddIdentityServer4(this IServiceCollection services)
        {
            var builder = services.AddIdentityServer(options =>
                {
                    options.Events.RaiseErrorEvents = true;
                    options.Events.RaiseInformationEvents = true;
                    options.Events.RaiseFailureEvents = true;
                    options.Events.RaiseSuccessEvents = true;
                })
                .AddDeveloperSigningCredential() //添加开发人员签名凭据,这仅适用于没有证书可以使用的开发场景。
                .AddInMemoryIdentityResources(IdentityServer4Config.GetIdentityResources()) //添加内存apiresource
                .AddInMemoryApiResources(IdentityServer4Config.GetApiResources())
                .AddInMemoryApiScopes(IdentityServer4Config.ApiScopes)
                .AddInMemoryClients(IdentityServer4Config.GetClients()); //把配置文件的client配置资源放到内存

            builder.AddTestUsers(new List<TestUser> { new TestUser { Username = "Admin", Password = "111111", IsActive = true, SubjectId = "1" } });

            //  .AddAuthorization(option =>
            //{
            //    option.AddPolicy("ApiScope", policy =>
            //    {
            //        policy.RequireAuthenticatedUser();
            //        policy.RequireClaim("scope", "api1");
            //    });
            //});

            return services;
        }

    }
}